import * as index from '@/api/index.js'
export default {
	data() {
		return {
			longitude: '', //中心经度
			latitude: '', //中心纬度
			form: {
				longitude: '', //经度
				latitude: '', //纬度
				address: '　'
			},
			covers: [], //地图点位数据
			scale: 16, //地图缩放比例
			token:'',
			loginForm: {
				code: '',
				mobilecode: ''
			},
			menuButtonInfo:uni.getMenuButtonBoundingClientRect(),
			city:'定位'
		}
	},

	onShow() {
		this.token = uni.getStorageSync('token')
	},
	onLoad(option) {
		let that = this
		uni.$on('location', function(data) {
			that.form = uni.getStorageSync('form')
		})
		this.getwxCode()
		this.getLocation()
	},
	onShareAppMessage(){
		
	},
	methods: {
		//获取当前位置
		getLocation() {
			let that = this
			uni.getLocation({
				type: 'gcj02',
				success: function(res) {
					that.longitude = res.longitude
					that.latitude = res.latitude
					that.form.longitude = res.longitude
					that.form.latitude = res.latitude
					that.covers = [{
						id: 1,
						latitude: res.latitude,
						longitude: res.longitude,
						iconPath: '../../static/home/mapIcon.png',
						callout: {
							content: '师傅预计' + Math.floor(Math.random() * 10 + 3) + '分钟内报价',
							color: '#000',
							fontSize: 12,
							borderRadius: 50,
							padding: 11,
							bgColor: '#fff',
							display: 'ALWAYS',
							textAlign: 'center'
						},
						width: 30,
						height: 45,
					}]
					that.getAmap(res.longitude, res.latitude)
				}
			});
		},
		//中心的移动的当前位置
		moveLocation() {
			uni.createMapContext('map').moveToLocation()
		},
		chooseLocation() {
			let that = this
			uni.chooseLocation({
				success: function(res) {
					that.form.address = res.address
					that.form.latitude = res.latitude
					that.form.longitude = res.longitude
					that.covers = [{
						id: 1,
						latitude: res.latitude,
						longitude: res.longitude,
						iconPath: '../../static/home/mapIcon.png',
						callout: {
							content: '师傅预计' + Math.floor(Math.random() * 10 + 3) + '分钟内报价',
							color: '#000',
							fontSize: 12,
							borderRadius: 50,
							padding: 11,
							bgColor: '#fff',
							display: 'ALWAYS',
							textAlign: 'center'
						},
						width: 30,
						height: 45,
					}]
					// uni.setStorageSync('form', that.form);
					uni.setStorage({
						key: 'form',
						data: that.form,
						success: function() {}
					});

				}
			});

		},
		jump(item) {
			this.$tools.navigate(item)
		},
		jumpOrder() {
			this.$tools.navigate('/pages/placeOrder/index')
		},
		//高德地图逆解析
		getAmap(lon, lat) {
			let that = this
			that.$myAmapFun.getRegeo({
				location: `${lon},${lat}`,
				success: function(data) {
					that.city = data[0].regeocodeData.addressComponent.city
					that.form.address = data[0].desc;
					uni.setStorage({
						key: 'form',
						data: that.form,
						success: function() {}
					});
				},
				fail: function(info) {
				}
			})
		},
		getKefuPhone(){
			index.getKefuPhone().then(res=>{
				uni.makePhoneCall({
					phoneNumber: res.data //仅为示例
				});
			})
		},
		getwxCode() {
			let that = this
			uni.login({
				provider: 'weixin',
				onlyAuthorize: true,
				success: function(loginRes) {
					that.loginForm.code = loginRes.code
				}
			});
		},
		getPhoneNumber(e) {
			this.loginForm.mobilecode = e.detail.code
			this.wxLogin()
		},
		wxLogin() {
			let that = this
			if (this.loginForm.mobilecode && this.loginForm.code) {
				index.wxmobile(this.loginForm).then(res => {
					uni.setStorage({
						key: 'userInfo',
						data: res.data.userinfo,
						success: function() {
						}
					})
					uni.setStorage({
						key: 'token',
						data: res.data.userinfo.token,
						success: function() {
							that.getwxCode()
							that.token = res.data.userinfo.token
							that.jumpOrder()
						}
					});
				})
			}
		},
	}
}